/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */


package ru.teleology.cubefs.editor.model;

import java.util.EventListener;
import java.util.List;

import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import ru.teleology.cubefs.editor.model.ArrayTreeNode.Provider;

public class ArrayTreeModel implements TreeModel {
	
    protected EventListenerList listenerList = new EventListenerList();
	private final List<?> list;
	
	private final ArrayTreeNode root;
    
    public ArrayTreeModel(List<?> container) {
		this.list = container;
		root = new ArrayTreeNode(new Provider() {
			@Override
			public Object getObject() {
				return list;
			}

			@Override
			public Object leaf(long index) {
				if (index < list.size()) {
					return list.get((int) index);
				}
				else {
					return -1;
				}
			}

			@Override
			public long size() {
				return list.size();
			}

			@Override
			public long getLeafIndex(Object leaf) {
				Integer i = (Integer) leaf;
				return i.longValue();
			}
		});
    }
    

	public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
		return listenerList.getListeners(listenerType);
	}

    public void addTreeModelListener(TreeModelListener l) {
        listenerList.add(TreeModelListener.class, l);
    }

    public void removeTreeModelListener(TreeModelListener l) {
        listenerList.remove(TreeModelListener.class, l);
    }

	@Override
	public Object getChild(Object parent, int index) {
		ArrayTreeNode node = (ArrayTreeNode) parent;
		return node.getChild(index);
	}

	

	@Override
	public int getChildCount(Object parent) {
		if (parent instanceof ArrayTreeNode) 
		{
			ArrayTreeNode node = (ArrayTreeNode) parent;
			return node.getChildrenCount();
		}
		else {
			return 0;
		}
	}

	@Override
	public int getIndexOfChild(Object parent, Object child) {
		ArrayTreeNode node = (ArrayTreeNode) parent;
		
		if (child instanceof ArrayTreeNode) {
			return node.getChildIndex((ArrayTreeNode)child);
		}
		else {
			Number i = (Number) child;
			return (int)(i.longValue() - node.getFrom()); 
		}
		
	}

	@Override
	public Object getRoot() {
		return root;
	}

	@Override
	public boolean isLeaf(Object node) {
		if (node instanceof ArrayTreeNode) {
			return false;
		}
		else {
			return true;
		}
	}

	@Override
	public void valueForPathChanged(TreePath path, Object newValue) {
		
	}
	
//	private Object[] getLastPath() {
//		int levels = root.levels() - 1;
//		Object[] path = new Object[levels];
//		
//		path[0] = root;
//		
//		IndexTreeNode node = root;
//		for (int c = 1; c < levels; c++) {
//			path[c] = node.getChild(node.getChildrenCount() - 1);
//			node = (IndexTreeNode) path[c];
//		}
//		
//		return path;
//	}
	
	public void fireUpdate() {
		Object[] listeners = listenerList.getListenerList();
        TreeModelEvent e = new TreeModelEvent(this, new Object[]{root});
        
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==TreeModelListener.class) {
                ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
            }          
        }
	}
}